﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Blazing.BusinessObjects.Cms;
using System.BusinessObjects.Data;
using System.Text.RegularExpressions;
using System.Web;
using Blazing.BusinessObjects.Queries;
using System.BusinessObjects.With;

namespace Blazing.WebFacade
{
    [System.ComponentModel.DataObject]
    public class RedirectController
    {
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeleteUrlRedirect(UrlRedirect obj)
        {
            UrlRedirect.Evict(obj.ID);
            UrlRedirect save = UrlRedirect.Load(obj.ID);

            if (save.Site != null)
                FacadeCacheManager.Invalidate<List<UrlRedirect>>(save.Site.ID);

            save.Delete();
            With.Transaction.Execute(() => save.Save());
            UrlRedirect.Evict(save.ID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public UrlRedirect NewUrlRedirect(UrlRedirect obj)
        {
            if (obj.Site != null)
                FacadeCacheManager.Invalidate<List<UrlRedirect>>(obj.Site.ID);
            obj.Save();
            return obj;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public UrlRedirect UpdateUrlRedirect(UrlRedirect obj)
        {
            if(obj.Site != null)
                FacadeCacheManager.Invalidate<List<UrlRedirect>>(obj.Site.ID);
            obj.Save();
            return obj;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public IList<UrlRedirect> GetUrlRedirect(int siteId, string sortExpression)
        {
            List<UrlRedirect> list = (List<UrlRedirect>)UrlRedirect.Search(QrySearchUrlRedirects.Query(siteId));
            if (list != null && sortExpression != String.Empty)
            {
                string[] sortOptions = sortExpression.Split(' ');
                if (sortOptions.Length > 0)
                {
                    string order = sortOptions.Length > 1 ? sortOptions[1] : "ASC";
                    GenericObjectComparer<UrlRedirect> comparer = new GenericObjectComparer<UrlRedirect>(sortOptions[0], order);
                    list.Sort(comparer);
                }
            }
            return list;
        }

        static object urlRedirectSyncObj = new object();
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public IList<UrlRedirect> GetUrlRedirect(int siteId)
        {
            List<UrlRedirect> retval = FacadeCacheManager.GetItem<List<UrlRedirect>, int>(arg0 => (List<UrlRedirect>)UrlRedirect.Search(QrySearchUrlRedirects.Query(arg0, true)),
                siteId, ref urlRedirectSyncObj, siteId);

            return retval;
        }

        public UrlRedirect GetMatchingRedirect(string url)
        {
            foreach (UrlRedirect r in UrlRedirect.Search())
            {
                if (r.IsActive)
                {
                    if (Regex.IsMatch(url, r.MatchPattern, RegexOptions.IgnoreCase))
                        return r;
                }
            }
            return null;
        }

        public void Redirect(UrlRedirect r, string currentUrl)
        {
            string destination = Regex.Replace(currentUrl, r.MatchPattern, r.DestinationResult);
            HttpContext.Current.Response.Redirect(destination, false);
            HttpContext.Current.Response.StatusCode = r.RedirectStatusCode;
            HttpContext.Current.Response.End();
        }
    }
}
